﻿using Qy.Core;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Qy.Scenario;

[AppService]
public class DocReceivedService : QyRepository<DocReceived>
{
    public DocReceivedService(ApplicationContext applicationContext) : base(applicationContext)
    {
    }
    public async Task<EditDocReceived> GetEditAsync(int? id)
    {
        //Sql sql = Sql.Builder;
        //sql.Select("doc.*,cate.Name as CateName")
        //    .From(DbTableName.OasDocReceived + " as doc")
        //    .LeftJoin(DbTableName.OasDocCat + " as cate").On("doc.Cid = cate.Id");
        //sql.Where("doc.Id =@0", id);
        //return await CreateDAO().FirstOrDefaultAsync<EditDocReceived>(sql);
        return await Queryable()
       .LeftJoin<DocCat>((data, cat) => data.Cid == cat.Id)
       .Select((data, cat) => new EditDocReceived()
       {
           CateName = cat.Name
       }, true)
       .FirstAsync(data => data.Id == id);
    }
    public async Task<PagedInfo<ViewDocReceivedAndCate>> GetPageListAsync(DocReceivedQuery parm)
    {
        //Sql sql = Sql.Builder;
        //sql.Select("cate.Name as CateName,dept.Name as DeptName,u.Nickname,doc.*")
        //    .From(DbTableName.OasDocReceived + " as doc")
        //    .LeftJoin(DbTableName.OasDocCat + " as cate").On("doc.Cid = cate.Id");
        //sql.LeftJoin(DbTableName.QyAdminUser + " u").On("doc.Uid=u.Id");
        //sql.LeftJoin(DbTableName.QyAdminDept + " dept").On("u.DeptId=dept.Id");
        //if (query.Miji > -1)
        //    sql.Where("doc.Miji = @0", query.Miji);
        //if (query.Uids == "own")
        //    sql.Where("doc.Uid = @0", query.Uid);
        //else if (query.Uids != null)
        //    sql.Where("doc.Uid IN (" + query.Uids + ")");
        //if (query.Cid != "0" && query.Cid != null)
        //    sql.Where("doc.Cid =@0", int.Parse(query.Cid));

        //if (!string.IsNullOrEmpty(query.Title))
        //    sql.Where("doc.Title like @0", "%" + query.Title + "%");
        //if (!string.IsNullOrEmpty(query.Clercator))
        //    sql.Where("doc.Clercator like @0", "%" + query.Clercator + "%");
        //if (!string.IsNullOrEmpty(query.DocNumber))
        //    sql.Where("doc.DocNumber like @0", "%" + query.DocNumber + "%");
        //if (!string.IsNullOrEmpty(query.Department))
        //    sql.Where("doc.Department like @0", "%" + query.Department + "%");
        //sql.OrderBy("doc.Id desc");
        //return await CreateDAO().PageAsync<ViewDocReceivedAndCate>(query.Page, query.Limit, sql);

        var exp = Expressionable.Create<DocReceived>();
        exp = exp.AndIF(parm.Miji > -1, data => data.Miji == parm.Miji);
        exp = exp.AndIF(parm.Cids != null, data => parm.Cids.Contains(data.Cid));
        if (parm.Uids == null)
            exp = exp.And(data => data.Uid == parm.Uid);
        else
            exp = exp.AndIF(!parm.Uids.Exists(x => x == -1), data => parm.Uids.Contains(data.Uid));

        return await Queryable()
            .LeftJoin<DocCat>((data, cat) => data.Cid == cat.Id)
            .LeftJoin<User>((data, cat, user) => data.Uid == user.Id)
            .LeftJoin<UserDept>((data, cat, user, dept) => user.DeptId == dept.Id)
            .Where(exp.ToExpression())
            .WhereIF(!string.IsNullOrEmpty(parm.Title), (data, cat) => data.Title.Contains(parm.Title))
            .WhereIF(!string.IsNullOrEmpty(parm.Clercator), (data, cat) => data.Clercator.Contains(parm.Clercator))
            .WhereIF(!string.IsNullOrEmpty(parm.DocNumber), (data, cat) => data.DocNumber.Contains(parm.DocNumber))
            .WhereIF(!string.IsNullOrEmpty(parm.Department), (data, cat) => data.Department.Contains(parm.Department))
            .OrderBy((data) => data.Id, OrderByType.Desc)
            .Select((data, cat, user, dept) => new ViewDocReceivedAndCate()
            {
                CateName = cat.Name,
                DeptName = dept.Name,
                Nickname = user.NickName,
                Id = data.Id.SelectAll(),
            })
            .ToPageAsync(parm);
    }

}
